﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml;
using Ionic.Zip;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Threading;

namespace LegoBrickList.Forms
{
    /// <summary>
    /// The main form of the application.
    /// </summary>
    public partial class MainForm : Form
    {
        private LddFile lddFile;
        private Thread thread;
        private WaitForm waitForm = new WaitForm();

        /// <summary>
        /// Initializes the main form.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
            AddVersionToTitle();
        }

        /// <summary>
        /// Adds the application version to the title.
        /// </summary>
        private void AddVersionToTitle()
        {
            string version = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion;
            Text = string.Format("{0} - {1}", Text, version);
        }

        /// <summary>
        /// Raised when a file open button is pressed.
        /// </summary>
        private void openButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // Reset labels
                mainProgressBar.Visible = true;
                mainStatusLabel.Visible = true;

                using (Busy busy = new Busy())
                {
                    if (this.lddFile == null)
                        this.lddFile = new LddFile();

                    this.thread = new Thread(LoadFile);
                    this.thread.Start(openFileDialog.FileName);
                    loadTimer.Enabled = true;
                    if (waitForm.ShowDialog() == DialogResult.Cancel)
                    {
                        thread.Abort();
                        loadTimer.Enabled = false;
                        mainProgressBar.Visible = false;
                        mainStatusLabel.Visible = false;
                    }
                }
            }
        }

        private void LoadFile(object fileName)
        {
            this.lddFile.Load(fileName.ToString());
        }

        private void ShowFile()
        {
            fileNameLabel.Text = openFileDialog.FileName;
            brickCountLabel.Text = "0";
            uniqueBrickCountLabel.Text = "0";
            partGridView.Rows.Clear();

            if (this.lddFile.HasErrors)
            {
                MessageBox.Show(this.lddFile.ErrorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                // Fill picture box.
                if (string.IsNullOrEmpty(this.lddFile.ImageFileName))
                    legoPictureBox.ImageLocation = null;
                else
                    legoPictureBox.ImageLocation = this.lddFile.ImageFileName;

                // Fill the grid                        
                foreach (var part in this.lddFile.LegoParts)
                    partGridView.Rows.Add(new object[] { part.Number, part.Description, part.Color, part.Count });
                partGridView.Sort(partGridView.Columns[0], ListSortDirection.Ascending);

                // Fill labels
                brickCountLabel.Text = this.lddFile.TotalPartCount.ToString();
                uniqueBrickCountLabel.Text = this.lddFile.UniquePartCount.ToString();
            }
            mainProgressBar.Visible = false;
            mainStatusLabel.Visible = false;

        }

        /// <summary>
        /// Raised when a show about box button is pressed.
        /// </summary>
        private void aboutButton_Click(object sender, EventArgs e)
        {
            using (AboutForm form = new AboutForm())
            {
                form.ShowDialog(this);
            }
        }

        /// <summary>
        /// Raised when a exit button is pressed.
        /// </summary>
        private void exitButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Raised when a help button is pressed.
        /// </summary>
        private void helpButton_Click(object sender, EventArgs e)
        {
            Process.Start("http://legobricklist.codeplex.com/documentation");
        }

        /// <summary>
        /// Raised when a copy button is pressed.
        /// </summary>
        private void copyButton_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(partGridView.GetClipboardContent());
        }

        private void loadTimer_Tick(object sender, EventArgs e)
        {
            if (thread == null)
            {
                loadTimer.Enabled = false;
                return;
            }

            if (!thread.IsAlive)
            {
                ShowFile();
                waitForm.Close();
                loadTimer.Enabled = false;
                return;
            }
        }
    }
}
